<?php
namespace app\cms\model;
/* *
 * 内容管理
 * */
class index extends \app\base\model\AdminModel{
    protected $table = 'cms_content';  //文章表

    //查询
    public function _select($cid,$website_id){
        $all = model('channel')->AllChildClass($cid,$website_id);
        $condition[] = 'channel_id in('.$all.')';
        return $this->table('cms_content as A')
            ->join('{pre}cms_class as B ON A.channel_id = B.id')
            ->field('A.channel_id,A.title,A.id,A.uptime,A.views,A.sort,A.status,A.fontcolor,B.name as channel_name')
            ->where($condition)
            ->order('a.sort asc,a.id desc')
            ->select();
    }

    //单个
    public function _find($condition,$field = NULL){
        return $this->where($condition)->field($field)->find();
    }

    //删除
    public function _delete($fid){
        //查询所有关联表
        $info = $this->_find(array('id'=>$fid),'channel_id');
        $tables = model('channel')->_model($info['channel_id']);
        $channel = model('channel')->_find(array('id'=>$info['channel_id']),'plus_mid');
        //删除
        $rel = $this->where(array('id'=>$fid,'channel_id' =>$info['channel_id']))->delete();
        if($rel){
            $result = $this->table($tables)->where(array('content_id'=>$fid,'channel_id' =>$info['channel_id']))->delete();
            if($channel['plus_mid']){
                $ext_tables = model('module')->tables($channel['plus_mid']);
                $ext_result = $this->table($ext_tables)->where(array('content_id'=>$fid,'channel_id' =>$info['channel_id']))->delete();
            }   
        }
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }   
    }

    //编辑
    public function _update($data,$condition){
        return $this->where($condition)->data($data)->update();
    } 

   //编辑
   public function _edit($fid){
        $fid = intval($fid);
        $info = $this->where(array('id'=>$fid))->find();
        if($info['channel_id']){
            $tables = model('channel')->_model($info['channel_id']);
            $ext_info = $this->table($tables)->where(array('content_id'=>$fid))->find();
            if($ext_info){
                return array_merge($info,$ext_info);
            }else{
                return FALSE;
            }

        }else{
            return FALSE;
        }    
   }

   //增加
   public function dataSave($mid,$postinput,$fid = null){
        $data['channel_id'] = $postinput['channel_id'];
        $data['title'] = trim($postinput['title']);
        $data['sort'] = trim($postinput['sort']);
        $data['views'] = trim($postinput['views']);
        $data['url'] = trim($postinput['url']);
        $data['uptime'] = $postinput['uptime'];
        $data['editor'] = trim($postinput['editor']);
        $data['fontcolor'] = trim($postinput['fontcolor']);
        $data['status'] = 1;
        if($postinput['special']){
            $data['special'] = implode(',', $postinput['special']);
        }
        $ext_data = filter_string($postinput,array('channel_id','title','sort','views','url','special','uptime','editor','fontcolor','status'));
        $mid  = intval($mid);
        $table = model('module')->tables($mid);
        if($fid){
            $condition['id'] = $fid;
            $rel = $this->where($condition)->data($data)->update();
            if($rel){
                $ext_data['channel_id'] = $postinput['channel_id'];
                $result = $this->table($table)->where(array('content_id'=>$fid))->data($ext_data)->update();
            }       
        }else{
            $rel = $this->data($data)->insert();
            if($rel){
                $ext_data['content_id'] = $rel;
                $ext_data['channel_id'] = $postinput['channel_id'];
                $result = $this->table($table)->data($ext_data)->insert();
            }
        }
        return $result ;
    }
    //###########################################
    //判断是否有扩展
    public function exts_Count($cid,$fid){
        $cid = intval($cid);
        $fid = intval($fid);
        $mid = model('channel')->_find(array('id'=>$cid),'plus_mid');
        if($mid['plus_mid']){
            $tables = model('module')->tables($mid['plus_mid']);
            $count = $this->table($tables)->where(array('content_id' =>$fid,'channel_id' =>$cid))->count();
            echo ' <a class="red" href="'.url('index/exts',array('mid'=>$rel,'cid'=>$cid,'id'=>$fid)).'">[ '.$count.' ]</a>';
        }
    }

    //扩展内容列表
    public function exts_select($cid,$id){
        $mid = model('channel')->_find(array('id'=>$cid),'plus_mid');
        $table = model('module')->tables($mid['plus_mid']);
        $rel = $this->table($table)->where(array('content_id' =>$id,'channel_id' =>$cid))->field('id,content_id,channel_id,title,uptime,fontcolor,views,sort,url')->order('id desc')->select();
        return $rel;
    }

   //编辑扩展内容
   public function extsedit($mid,$fid){
        $tables = model('module')->tables(intval($mid));
        return $this->table($tables)->where(array('id'=>intval($fid)))->find();
    }

    //扩展内容保存
    public function extssave($postinput,$channel_id,$content_id,$fid = NULL){
        $channel = model('channel')->_find(array('id'=>$channel_id),'plus_mid,id');
        $table = model('module')->tables($channel['plus_mid']);
        $data = $postinput;
        $data['uptime'] = time();
        if($fid){
             return $this->table($table)->where(array('id' => $fid))->data($data)->update();
        }else{
             return $this->table($table)->data($data)->insert();
        }
   }

    //删除文章
   public function extsdelete($cid,$fid){
        $channel = model('channel')->_find(array('id'=>$cid),'plus_mid,id');
        $table = model('module')->tables($channel['plus_mid']);
        return $this->table($table)->where(array('id' => $fid))->delete();
   }
}